package cn.chenxiejia;

import java.io.*;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: Task1
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/2/11 14:41
 */
public class Task1 {
    public static void main(String[] args) {
        try {
            //append:true追加写入
            BufferedWriter bw = new BufferedWriter(new FileWriter("aa1.txt", true));
            for (int i = 0; i < 10; i++) {
                bw.append("holle world!中文效果\n");
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("字符流：");
        var m1 = System.currentTimeMillis();
        try {
            BufferedReader br = new BufferedReader(new FileReader("aa1.txt"));
            while (br.ready()) {
                System.out.println(br.readLine());
            }
            System.out.printf("字符流时间：%d毫秒%n", System.currentTimeMillis() - m1);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("字节流：");
        try {
            var m2 = System.currentTimeMillis();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream("aa1.txt"));
            byte[] s = new byte[1024];
            int aa = 0;
            while ((aa = bis.read(s)) > 0) {
                System.out.println(new String(s, 0, aa));
            }
            bis.close();

            System.out.printf("字节流时间：%d毫秒%n", System.currentTimeMillis() - m2);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
